//
//  WBToolBarView.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/3.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit

class WBToolBarView: UIView {
    var retweetButton: UIButton!
    var commentButton: UIButton!
    var unlikeButton: UIButton!
    
    var statusViewModel : WBStatusViewModel?
    {
        didSet {
            //retweetButton.setTitle("\(statusViewModel?.status?.reposts_count ?? 0)", for: UIControlState.normal)
            //commentButton.setTitle("\(statusViewModel?.status?.comments_count ?? 0)", for: UIControlState.normal)
            //unlikeButton.setTitle("\(statusViewModel?.status?.attitudes_count ?? 0)", for: UIControlState.normal)
            retweetButton.setTitle(statusViewModel?.reposts_count, for: UIControlState.normal)
            commentButton.setTitle(statusViewModel?.comments_count, for: UIControlState.normal)
            unlikeButton.setTitle(statusViewModel?.attitudes_count, for: UIControlState.normal)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUI()
    {
        //self.backgroundColor = UIColor.blue
        //添加按钮
        retweetButton = self.addButton(imageName: "timeline_icon_retweet", defaultTitle: "转发")
        commentButton = self.addButton(imageName: "timeline_icon_comment", defaultTitle: "评论")
        unlikeButton = self.addButton(imageName: "timeline_icon_unlike", defaultTitle: "赞")
        //添加按钮分割线
        let p1:UIImageView = UIImageView(image: #imageLiteral(resourceName: "timeline_card_bottom_line"))
        let p2:UIImageView = UIImageView(image: #imageLiteral(resourceName: "timeline_card_bottom_line"))
        
        self.addSubview(p1);self.addSubview(p2)
        
        //设置约束
        retweetButton.snp_makeConstraints { (make)->Void in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        commentButton.snp_makeConstraints { (make)->Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(retweetButton.snp_right)
            make.width.equalTo(retweetButton)
        }
        unlikeButton.snp_makeConstraints { (make)->Void in
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(commentButton.snp_right)
            make.width.equalTo(commentButton)
        }
        p1.snp_makeConstraints { (make)->Void in
            make.centerX.equalTo(retweetButton.snp_right)
            make.centerY.equalTo(retweetButton)
        }
        p2.snp_makeConstraints { (make)->Void in
            make.centerX.equalTo(commentButton.snp_right)
            make.centerY.equalTo(commentButton)
        }
    }
    
    //添加按钮的公共方法
    private func addButton(imageName:String,defaultTitle:String)->UIButton{
        let btn = UIButton()
    btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: UIControlState.normal)
    btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background_highlighted"), for: UIControlState.highlighted)
        btn.setImage(UIImage(named:imageName), for: UIControlState.normal)
        btn.setTitle(defaultTitle, for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(btn)
        return btn
        
    }

}
